package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Random;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;

import com.example.fw.ApplicationManager;
import com.example.fw.ContactObject;
import com.example.fw.GroupObject;
import com.example.fw.Groups;

public class ContactDataGenerator {
		
	static Random rnd = new Random();
	
	@DataProvider(name = "randomContacts")
	public Iterator<Object[]> generateRandomContacts()	{
		List<ContactObject> contacts = new ContactDataGenerator().generateRandomContactList(null, 5);
		List<Object[]> list = new ArrayList<Object[]>();
		for (ContactObject contact : contacts) {
			list.add(new Object[]{contact, true});
		}
		return list.iterator();
	}

	@DataProvider(name = "contactsFromFile")
	public static Iterator<Object[]> loadContactsFromFile() throws IOException	{
		List<Object[]> list = new ArrayList<Object[]>();
		BufferedReader reader = new BufferedReader ( new FileReader("contacts.txt"));
		String line = reader.readLine();
		while (line != null)	{
			String[] parts = line.split("\t");
			ContactObject contact = new ContactObject()
				.setName(parts[0])
				.setLastname(parts[1])
				.setAddress(parts[2])
				.setHomePhone(parts[3])
				.setMobilePhone(parts[4])
				.setWorkPhone(parts[5])
				.setEmail(parts[6])
				.setEmail2(parts[7])
				.setBirthDay(parts[8])
				.setBirthMonth(parts[9])
				.setBirthYear(parts[10])
				.setGroup(parts[11])			
				.setAddress2(parts[12])
				.setPhone2(parts[13]);
			list.add(new Object[]{contact, true});
			line = reader.readLine();
		}
		reader.close();
		return list.iterator();
	}

	public static void main(String[] args)	{
		if (args.length < 2)	{
			System.out.println("Specify two parameters: file and count");
			return;
		}
		String file = args[0];
		int count = Integer.parseInt(args[1]);
		try {
			new ContactDataGenerator().generateDataToFile(file, count);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void generateDataToFile(String fileName, int count) throws IOException {
		List<ContactObject> contacts = generateRandomContactList(null, count);
		File file = new File(fileName);
		if (file.exists())	{
			file.delete();
			//System.out.println("File exists, stop generator");
			//return;
		}
		FileWriter writer = new FileWriter(file);
		for (ContactObject contact : contacts) {
			writer.write(contact.firstname + "\t" + contact.lastname + "\t" + contact.address + "\t" + contact.ph_home + "\t" + contact.ph_mobile + "\t" + contact.ph_work + "\t" + contact.email + "\t" + contact.email2 + "\t" + contact.bday + "\t" + contact.bmonth + "\t" + contact.byear + "\t" + contact.group + "\t" + contact.address2 + "\t" + contact.phone2 + "\n");
			//writer.write(group.toString());
			//writer.flush();
		}
		writer.close();
	}

	@Parameters({"configFile"})
	private List<ContactObject> generateRandomContactList(@Optional String configFile, int count) {
		ApplicationManager app;
		if (configFile == null)	{
			configFile = System.getProperty("configFile");
		}
		if (configFile == null)	{
			configFile = System.getenv("configFile");
		}
		if (configFile == null)	{
			configFile = "application.properties";
		}
		Properties props = new Properties();
		try {
			props.load(new FileReader(configFile));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		app = ApplicationManager.getInstance();
		app.setProperties(props);
		List<ContactObject> list = new ArrayList<ContactObject>();
		for (int i = 0; i < count; i++) {			
			String month = ContactObject.MONTHS[new Random().nextInt(12)];
			Groups groups = app.getGroupHelper().getGroups();
			GroupObject someGroupFromList = groups.getSomeGroupFromList();
			ContactObject contact = new ContactObject()
				.setName("Name"+rnd.nextInt())
				.setLastname("Lastname"+rnd.nextInt())
				.setAddress("Address"+rnd.nextInt())
				.setHomePhone(""+rnd.nextInt())
				.setMobilePhone(""+rnd.nextInt())
				.setWorkPhone(""+rnd.nextInt())
				.setEmail("email"+rnd.nextInt()+"@google.com")
				.setEmail2("email2"+rnd.nextInt()+"@google.com")
				.setBirthDay(""+app.getContactHelper().generateRandomNumberBetween(1, 31))
				.setBirthMonth(month)
				.setBirthYear(""+app.getContactHelper().generateRandomNumberBetween(1900, 2012))
				.setGroup(""+someGroupFromList.name)
				.setAddress2("Address2-"+rnd.nextInt())
				.setPhone2(""+rnd.nextInt());
				
			list.add(contact);
		}
		ApplicationManager.getInstance().stop();
		return list;
	}
}